﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quanlytruongtruongtieuhoc
{
    public partial class frmXemdanhsachlop : Form
    {
        public static string mahsxem;
        public static string mahs, makhoi;
        public frmXemdanhsachlop()
        {
            InitializeComponent();
        }

        private void frmXemdanhsachlop_Load(object sender, EventArgs e)
        { 
            try
            {
                string ma;
                if (frmXeplop.xem == 1)
                {
                    ma = frmXeplop.lophoc;
                    lbLop.Text = ma;
                }
                else if (frmXeplop.xem == 2)
                {
                    ma = frmXeplop.xemlophoc;
                    lbLop.Text = ma;
                }
                else
                {
                    ma = frmXeplop.luuban;
                    lbLop.Text = ma;
                }
              
                SqlCommand com=new SqlCommand("select max(nam_hoc) from nam_hoc",clsKetnoi.con);
                string nam=com.ExecuteScalar().ToString().Substring(2,2);
                nam=nam+"%%%%";
                SqlDataAdapter ap = new SqlDataAdapter("select a.ma_hs,a.ho,a.ten,a.gioi_tinh,convert(char(10),a.nam_sinh,103) from hoc_sinh a,lop_hoc_sinh b where a.ma_hs=b.ma_hs and nam_hoc=(select max(nam_hoc) from nam_hoc) and ma_khoi='"+ma.Substring(0,1)+"' and ma_lop='"+ma.Substring(2,1)+"' and a.xoa='False'", clsKetnoi.con);
                DataTable hs = new DataTable();
                hs.Clear();
                dtDanhsachhocsinh.Rows.Clear();
                ap.Fill(hs);
                for (int i = 0; i < hs.Rows.Count; i++)
                {
                    DataRow row = hs.Rows[i];
                    dtDanhsachhocsinh.Rows.Add();
                    dtDanhsachhocsinh.Rows[i].Cells[0].Value = i + 1;
                    dtDanhsachhocsinh.Rows[i].Cells[1].Value = row[0].ToString();
                    string ten = row[1].ToString() + " " + row[2].ToString();
                    dtDanhsachhocsinh.Rows[i].Cells[2].Value = ten;
                    if (row[3].ToString() == "Nam")
                        dtDanhsachhocsinh.Rows[i].Cells[3].Value = "Nam";
                    else dtDanhsachhocsinh.Rows[i].Cells[3].Value = "Nữ";
                    dtDanhsachhocsinh.Rows[i].Cells[4].Value = row[4].ToString();
                }
                mahsxem = lbLop.Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
        }
        private void xoaCSDL(string ma, string nam)
        {
            try
            {
                SqlCommand com = new SqlCommand("delete from diem_tong_hop where ma_hs='" + ma + "' and nam_hoc='" + nam + "'", clsKetnoi.con);
                SqlCommand com1 = new SqlCommand("delete from kiem_tra_dinh_ky where ma_hs='" + ma + "' and nam_hoc='" + nam + "'", clsKetnoi.con);
                SqlCommand com2 = new SqlCommand("delete from kiem_tra_danh_gia where ma_hs='" + ma + "' and nam_hoc='" + nam + "'", clsKetnoi.con);
                SqlCommand com3 = new SqlCommand("delete from lop_hoc_sinh where ma_hs='" + ma + "' and nam_hoc='" + nam + "'", clsKetnoi.con);
                com.ExecuteNonQuery();
                com1.ExecuteNonQuery();
                com2.ExecuteNonQuery();
                com3.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btXoa_Click(object sender, EventArgs e)
        {
            DialogResult kq = new DialogResult();
            string ma, nam;
            kq = MessageBox.Show("Có chắc xóa không?", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (kq == DialogResult.OK)
            {
                int a=dtDanhsachhocsinh.CurrentRow.Index;
                ma = dtDanhsachhocsinh.Rows[a].Cells[1].Value.ToString();
                nam = frmPhanconggvchunhiem.layNam();
                xoaCSDL(ma, nam);
                MessageBox.Show("Đã xóa thành công.", "Thông tin", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                dtDanhsachhocsinh.Rows.RemoveAt(a);
            }
        }

        private void btIn_Click(object sender, EventArgs e)
        {
            frmIndanhsachhocsinh A = new frmIndanhsachhocsinh();
            A.ShowDialog();
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            int a=dtDanhsachhocsinh.CurrentRow.Index;
            mahs = dtDanhsachhocsinh.Rows[a].Cells[1].Value.ToString();
            makhoi = lbLop.Text;
            frmCapnhatxeplop A = new frmCapnhatxeplop();
            A.ShowDialog();
            frmXemdanhsachlop_Load(sender, e);
        }
    }
}
